import state from './state';
import isPlainObject from 'is-plain-object';

const mutations = {};

for (const stateName in state) {
  const mutationName = `SAVE_${stateName}`;
  mutations[mutationName] = function(state, payload) {
    let val = payload;
    if (isPlainObject(payload) && payload.type === mutationName) {
      if (stateName in payload) {
        val = payload[stateName];
      } else {
        val = { ...payload };
        delete val.type;
      }
    }
    state[stateName] = val;
  };
}

export default mutations;
